﻿namespace 对象集合去重
{
    internal class Program
    {
        static void Main(string[] args)
        {
           var books = new List<Book>();
            books.Add(new Book() { Id = 1, Name = "C#" });
            books.Add(new Book() { Id = 1, Name = "C#" });
            books.Add(new Book() { Id = 2, Name = "JAVA" });
            books.Add(new Book() { Id = 2, Name = "JAVA" });
            books.Add(new Book() { Id = 3, Name = "Python" });

            var distinctBooks = books.Distinct();
            foreach (var book in distinctBooks)
            {
                Console.WriteLine(book.ToString());
            }
        }
    }
    class Book: IEquatable<Book>
    {
        public int Id;
        public string? Name;

        public bool Equals(Book? other)
        {
            return other!=null && Id == other.Id && Name==other.Name;
        }
        public override int GetHashCode()
        {
            return HashCode.Combine(Id, Name);
        }
        public override string ToString()
        {
            return $"Id={Id},Name={Name}";
        }
    }
}
